﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using Clandestine;
using Tao.Platform.Windows;
using Tao.OpenGl;
using Slick.MapEditor;

namespace Slick
{
    public partial class frmSlick : Form
    {
        public SimpleOpenGlControl GlControl;
        public GlControlManager GlManager;
        private string storedFilename = string.Empty;
        private int moveVelocity = 5;

        public frmSlick()
        {
            InitializeComponent();

            // Setup Gl Control
            GlControl = new SimpleOpenGlControl();
            GlControl.Dock = DockStyle.Fill;
            GlControl.Paint += new PaintEventHandler(GlControl_Paint);
            GlControl.KeyPress += new KeyPressEventHandler(GlControl_KeyPress);
            GlControl.MouseMove += new MouseEventHandler(GlControl_MouseMove);
            GlControl.MouseDown += new MouseEventHandler(GlControl_MouseDown);
            GlControl.MouseUp += new MouseEventHandler(GlControl_MouseUp);
            ControlContainer.Controls.Add(GlControl);

            // Setup GlManager
            GlManager = new GlControlManager(GlControl);
            Clandestine.Graphics.Initialise(1, GlManager);
            Clandestine.Graphics.PerformSlowDebugDepthChecking = false;

            mapSidePanel1.Initialise(((MapMode)Editor.CurrentMode).Map);
        }

        void GlControl_MouseUp(object sender, MouseEventArgs e)
        {
            Slick.Editor.MouseButtonCallback((e.Button == MouseButtons.Left) ? 0 : 1, 0);
        }

        void GlControl_MouseDown(object sender, MouseEventArgs e)
        {
            Slick.Editor.MouseButtonCallback((e.Button == MouseButtons.Left) ? 0 : 1, 1);
        }

        void GlControl_MouseMove(object sender, MouseEventArgs e)
        {
            Slick.Editor.MousePositionCallback(e.X, e.Y);
        }

        void GlControl_KeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case 'w': // Up
                    Camera.Position.Y -= moveVelocity;
                    break;
                case 'a': // Left
                    Camera.Position.X -= moveVelocity;
                    break;
                case 's': // Down
                    Camera.Position.Y += moveVelocity;
                    break;
                case 'd': // Right
                    Camera.Position.X += moveVelocity;
                    break;
            }
        }

        // Basically our OnRender!
        private void GlControl_Paint(object sender, PaintEventArgs e)
        {
            // Todo: Reimplement grid - is it even used?!

            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();

            // Setup view port
            int w, h;
            Clandestine.Graphics.Manager.GetViewportSize(out w, out h);
            Gl.glOrtho(0, w, h, 0, -1000, 1000);

            // Render biatcchh!
            Clandestine.Graphics.Render();
        }
        


        public void Open()
        {
            // Present dialog
            if (openDialog.ShowDialog() == DialogResult.OK)
                storedFilename = openDialog.FileName;
            else
            {
                MessageBox.Show("You must select a valid file to open!", "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ((MapMode)Editor.CurrentMode).Map.Load(storedFilename);
        }

        #region Form Events

        private void frmSlick_FormClosed(object sender, FormClosedEventArgs e)
        {
            Clandestine.Graphics.Deinitialise();
        }

        private void mnuOpen_Click(object sender, EventArgs e)
        {
            Open(); // call another func as we'll have tool bars and all sorts opening shizz
        }

        #endregion

        private void tmrRepaint_Tick(object sender, EventArgs e)
        {
            GlControl.Invalidate();
        }

        private void frmSlick_Load(object sender, EventArgs e)
        {

        }

    }
}
